package com.shangrila.common.lang;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.shangrila.common.exceptions.IORuntimeException;
/**
 * 用于属性文件的读取，属性文件需置于classpath中
 * @author kalman03
 */
public class PropertyObject {

	private static Properties properties;
	
	/**Property文件名，包括后缀名，请确保文件位于classpath中*/
	public PropertyObject(String fileName){
		try {
			PropertyObject.properties = initProperties(fileName);
		} catch (IOException e) {
			throw new IORuntimeException("Properties file '"+fileName+"' not in classpath or load property exception. ",e);
		}
	}
	
	private Properties initProperties(String fileName) throws IOException{
		InputStream inputStream = null;
		try {
			Properties prop = new Properties();
			inputStream = PropertyObject.class.getClassLoader().getResourceAsStream(fileName);
			prop.load(inputStream);
			return prop;
		}finally{
			if(inputStream != null){
				try {
					inputStream.close();
				} catch (IOException e) {
					//ignore
				}
			}
		}
	}
	
	public static String getString(String key){
		return properties.getProperty(key);
	}
	public static String getString(String key,String defaultValue){
		return properties.getProperty(key, defaultValue);
	}
	public static Object getObject(String key){
		return properties.get(key);
	}
}
